import os
import time
import signal


def work():
    f = open('/tmp/log', 'w')
    for i in range(100):
        print('writing %s' % i)
        f.write('%s\n' % i)
        time.sleep(1.5)
    f.close()


def sigterm_handler(signum, stackframe):
    print('signal number %s captured' % signum)
    exit(0)


def sigtstp_handler(signum, stackframe):
    print('偏不停')


def sigint_handler(signum, stackframe):
    print('不理你')


if __name__ == '__main__':
    # 打印出pid，方便测试
    print('pid:', os.getpid())

    # 设置信号的相应函数
    signal.signal(signal.SIGTERM, sigterm_handler)
    signal.signal(signal.SIGINT, sigint_handler)
    signal.signal(signal.SIGTSTP, sigtstp_handler)
    work()
